package fr.babostesting.unit.components.webview
{
	import com.greensock.easing.Strong;
	import com.greensock.TweenMax;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	import flash.utils.Timer;
	import fr.babos.core.entries.Document;
	import fr.babos.graphic.components.base.StageWrapper;
	import fr.babos.graphic.components.bitmaps.AdvancedBitmap;
	import fr.babos.graphic.components.webview.WebViewContainer;
	import net.hires.debug.Stats;
	
	/**
	 * @author ZoulouX
	 */
	public class WebViewTest extends Document
	{
		protected var _wrapper:StageWrapper;
		protected var _webView:WebViewContainer;
		protected var _btn1:AdvancedBitmap;
		protected var _btn2:AdvancedBitmap;
		protected var _btn3:AdvancedBitmap;
		
		public function WebViewTest ()
		{
			
		}
		
		override public function init ():void
		{
			_wrapper = new StageWrapper(stage);
			
			var back:AdvancedBitmap = new AdvancedBitmap(null, null, 1, 0xCCCCCC);
			back.place(0, 0, 0, 0).into(_wrapper);
			
			_webView = new WebViewContainer();
			
			_webView.additionalStruct += '<img width="100%" src="http://upload.wikimedia.org/wikipedia/commons/8/8b/Light_dispersion_conceptual.gif" />';
			
			_webView.additionalScript += <![CDATA[
				function responseTo (p1, p2, p3)
				{
					sendMessage("response", [p1, p2, p3]);
				}
			]]>.toString();
			
			_webView.place(110, 10, 50, 10).into(_wrapper);
			
			_webView.registerMessage("response", messageResponseHandler);
			
			_webView.autoRasterize = true;
			
			stage.addEventListener(MouseEvent.CLICK, stageClickHandler);
			
			var frontElement:AdvancedBitmap = new AdvancedBitmap(null, null, 1, 0xFF0000);
			frontElement.place(NaN, 0, NaN, 0).center(NaN, 0).size(NaN, 30).into(_wrapper);
			
			//setInterval(_webView.updateRaster, 100);
			
			_wrapper.addChild(new Stats());
			
			_btn1 = new AdvancedBitmap(null, null, 1, 0xFF0000);
			_btn1.place(20, NaN, NaN, 10).size(80, 80).into(_wrapper);
			
			_btn2 = new AdvancedBitmap(null, null, 1, 0xFF0000);
			_btn2.place(20, NaN, NaN, NaN).center(0, NaN).size(80, 80).into(_wrapper);
			
			_btn3 = new AdvancedBitmap(null, null, 1, 0xFF0000);
			_btn3.place(20, 10, NaN, NaN).size(80, 80).into(_wrapper);
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		protected function enterFrameHandler (event:Event):void
		{
			if (_webView != null && _webView.rasterize)
			{
				_webView.updateRaster();
			}
		}
		
		protected function messageResponseHandler (p1:Number, p2:Boolean, p3:String):void
		{
			trace("RECEIVED", p1, p2, p3);
			
			_webView.callJavascript("alert", ["RECEIVED AS3 -> JS -> AS3 : " + p1 + " - " + p2 + " - " + p3]);
		}
		
		protected function stageClickHandler (event:MouseEvent):void
		{
			if (event.target == _btn1)
			{
				trace("RASTERIZE", !_webView.rasterize);
				
				_webView.rasterize = !_webView.rasterize;
			}
			else if (event.target == _btn2)
			{
				_webView.callJavascript("responseTo", [5, false, "test, \"de @ ( ' # \' la // mort,'\""]);
			}
			else if (event.target == _btn3)
			{
				randomizeWebViewSize();
			}
		}
		
		protected function randomizeWebViewSize ():void
		{
			TweenMax.to(_webView, .6, {
				top: 110 + Math.random() * 60,
				right: 10 + Math.random() * 60,
				bottom: 50 + Math.random() * 60,
				left: 10 + Math.random() * 60,
				ease: Strong.easeInOut
			});
		}
	}
}